unit ArmDet;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Root, ComCtrls, ExtCtrls, StdCtrls, DBCtrls, Buttons,
  DB, IBCustomDataSet, IBQuery;

type
  TfmArmDet = class(TfmRoot)
    Panel1: TPanel;
    Panel2: TPanel;
    Label1: TLabel;
    sbAdd: TSpeedButton;
    sbDelete: TSpeedButton;
    sbTop: TSpeedButton;
    sbDown: TSpeedButton;
    Label2: TLabel;
    Label3: TLabel;
    qMenus: TIBQuery;
    dsMenus: TDataSource;
    cbMenus: TComboBox;
    qMenuItems: TIBDataSet;
    qMenuItemsMENUNO: TIntegerField;
    qMenuItemsFORMNO: TIntegerField;
    qMenuItemsORDINALNO: TIntegerField;
    qMenuItemsLABEL: TIBStringField;
    qAll: TIBDataSet;
    qAllFORMNO: TIntegerField;
    qAllLABEL: TIBStringField;
    GroupBox1: TGroupBox;
    qAllForms: TIBQuery;
    qAllFormsFORMNO: TIntegerField;
    qAllFormsLABEL: TIBStringField;
    lbCurrent: TListBox;
    lbAll: TListBox;
    qMenusMENUID: TIntegerField;
    qMenusLABEL: TIBStringField;
    procedure FormCreate(Sender: TObject);
    procedure cbMenusChange(Sender: TObject);
    procedure qMenuItemsAfterOpen(DataSet: TDataSet);
    procedure qAllAfterOpen(DataSet: TDataSet);
    procedure sbAddClick(Sender: TObject);
    procedure sbDeleteClick(Sender: TObject);
    procedure lbAllDblClick(Sender: TObject);
    procedure lbCurrentDblClick(Sender: TObject);
    procedure sbTopClick(Sender: TObject);
    procedure sbDownClick(Sender: TObject);
    procedure qMenuItemsBeforeClose(DataSet: TDataSet);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    IsNeedUpdate: Boolean;
  public
    { Public declarations }
  end;

var
  fmArmDet: TfmArmDet;

implementation

uses DBinv;

{$R *.dfm}

procedure TfmArmDet.FormCreate(Sender: TObject);
begin
  inherited;
  IsNeedUpdate:= False;
  if qMenus.Active then qMenus.Close;
  qMenus.Open;
  qMenus.FetchAll;
  cbMenus.Items.Clear;
  while not qMenus.Eof do begin
    cbMenus.Items.Add(qMenusLABEL.AsString);
    qMenus.Next;
  end;
  cbMenus.ItemIndex:= 0;
  cbMenusChange(cbMenus);
  if qMenuItems.Active then qMenuItems.Close;
  qMenuItems.Open;
  qMenuItems.FetchAll;
  if qAll.Active then qAll.Close;
  qAll.Open;
  qAll.FetchAll;
end;

procedure TfmArmDet.cbMenusChange(Sender: TObject);
begin
  qMenuItems.Close;
  qMenus.Locate('Label', cbMenus.Text, []);
  qMenuItems.Open;
end;

procedure TfmArmDet.qMenuItemsAfterOpen(DataSet: TDataSet);
begin
  lbCurrent.Items.Clear;
  while not qMenuItems.Eof do begin
    lbCurrent.Items.Add(qMenuItemsLABEL.AsString);
    qMenuItems.Next;
  end;
  IsNeedUpdate:= False;
end;

procedure TfmArmDet.qAllAfterOpen(DataSet: TDataSet);
begin
  lbAll.Items.Clear;
  while not qAll.Eof do begin
    lbAll.Items.Add(qAllLABEL.AsString);
    qAll.Next;
  end;
end;


procedure TfmArmDet.sbAddClick(Sender: TObject);
var
  str: String;
  ind: Integer;
begin
  if (lbAll.ItemIndex < 0) then Exit;
  ind:= lbAll.ItemIndex;
  str:= lbAll.Items[ind];
  lbCurrent.Items.Append(str);
  lbAll.DeleteSelected;
  IsNeedUpdate:= True;
end;

procedure TfmArmDet.sbDeleteClick(Sender: TObject);
var
  str: String;
  ind: Integer;
begin
  if (lbCurrent.ItemIndex < 0) then Exit;
  ind:= lbCurrent.ItemIndex;
  str:= lbCurrent.Items[ind];
  lbAll.Items.Append(str);
  lbCurrent.DeleteSelected;
  IsNeedUpdate:= True;
end;

procedure TfmArmDet.lbAllDblClick(Sender: TObject);
begin
  sbAddClick(sbAdd);
end;

procedure TfmArmDet.lbCurrentDblClick(Sender: TObject);
begin
  sbDeleteClick(sbDelete);
end;

procedure TfmArmDet.sbTopClick(Sender: TObject);
var
  li_index: Integer;
  ls_Text:  String;
begin
  if (lbCurrent.Count = 0) or (lbCurrent.ItemIndex = 0) or
     (lbCurrent.ItemIndex < 0) then Exit;
  li_index:= lbCurrent.ItemIndex;
  ls_Text:= lbCurrent.Items[li_index];
  lbCurrent.DeleteSelected;
  lbCurrent.Items.Insert(li_index - 1, ls_Text);
  lbCurrent.Selected[li_index - 1]:= True;
  IsNeedUpdate:= True;
end;

procedure TfmArmDet.sbDownClick(Sender: TObject);
var
  li_index: Integer;
  ls_Text:  String;
begin
  if (lbCurrent.Count = 0) or (lbCurrent.ItemIndex = lbCurrent.Count - 1) or
     (lbCurrent.ItemIndex < 0) then Exit;
  li_index:= lbCurrent.ItemIndex;
  ls_Text:= lbCurrent.Items[li_index];
  lbCurrent.DeleteSelected;
  lbCurrent.Items.Insert(li_index + 1, ls_Text);
  lbCurrent.Selected[li_index + 1]:= True;
  IsNeedUpdate:= True;
end;

procedure TfmArmDet.qMenuItemsBeforeClose(DataSet: TDataSet);
var i: Integer;
begin
  if not IsNeedUpdate then Exit;
  if qAllForms.Active then qAllForms.Close;
  qMenuItems.First;
  while not qMenuItems.Eof do qMenuItems.Delete;
  if (lbCurrent.Count > 0) then begin
    qAllForms.Open;
    qAllForms.FetchAll;
    qMenuItems.Edit;
    for i:= 0 to (lbCurrent.Count - 1) do begin
      qMenuItems.Insert;
      qMenuItemsMENUNO.AsInteger:= qMenusMENUID.AsInteger;
      qAllForms.Locate('Label', lbCurrent.Items[i], []);
      qMenuItemsFORMNO.AsInteger:= qAllFormsFORMNO.AsInteger;
      qMenuItemsORDINALNO.AsInteger:= i + 1;
    end;
  end;
  qMenuItems.ApplyUpdates;
  IsNeedUpdate:= False;
  if DMInv.IBTransaction1.InTransaction then DMInv.IBTransaction1.CommitRetaining;
  qAllForms.Close;
end;

procedure TfmArmDet.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  if qAllForms.Active then qAllForms.Close;
  if qAll.Active then qAll.Close;
  if qMenuItems.Active then qMenuItems.Close;
  if qMenus.Active then qMenus.Close;
end;

end.
